<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="isLook ? '查看' : '编辑'" v-model="visible" align-center
    width="1300px" :z-index="2000" @close="handleClose">
    <div class="v-title mb-3">基础信息</div>
    <el-form ref="formRef" :model="form" label-position="top" :disabled="isLook" :label-width="140">
      <div class="grid grid-cols-4 gap-x-3">
        <el-form-item label="供应商">
          <el-input v-model="form.basic.supplier_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="供应商类型">
          <el-input v-model="form.basic.purchase_type_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="结算方式">
          <el-input v-model="form.basic.period_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="公司主体">
          <el-input v-model="form.basic.company_id_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="退货类型">
          <VSelect v-model="form.basic.return_type" placeholder="请输入" :options="options.return_type || []" filterable
            clearable class="w-full" />
        </el-form-item>
        <el-form-item label="来源单号">
          <VSelect v-model="form.basic.department_id" placeholder="请输入" disabled :options="options.department_id || []"
            filterable clearable class="w-full" />
        </el-form-item>
        <el-form-item label="退货方式" prop="basic.return_method"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.basic.return_method" placeholder="请输入" :options="options.return_method || []" filterable
            clearable class="w-full" />
        </el-form-item>
        <el-form-item label="收件人" prop="basic.contact_name"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.basic.contact_name" placeholder="收件人" class="w-full" />
        </el-form-item>
        <el-form-item label="联系电话" prop="basic.contact_tel"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.basic.contact_tel" placeholder="联系电话" class="w-full" />
        </el-form-item>
        <el-form-item label="收货地址" prop="basic.return_area_id"
          :rules="[{ required: form.basic.return_method !== 2, message: '不能为空', trigger: 'change' }]" class="col-span-2">
          <Area v-model="form.basic" field="return" />
        </el-form-item>
        <el-form-item label="运费承担" prop="basic.tracking_carrier"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.basic.tracking_carrier" placeholder="请输入" :options="options.tracking_carrier || []"
            filterable clearable class="w-full" />
        </el-form-item>
        <el-form-item label="垫付运费" prop="is_prepay">
          <VSelect v-model="form.basic.is_prepay" placeholder="请输入" :options="options.is_prepay || []" filterable
            clearable class="w-full" />
        </el-form-item>
        <el-form-item label="退货运费" prop="transport_type">
          <el-input-number v-model="form.basic.tracking_price" controls-position="right" :min="0" :step="0.01"
            :precision="2" class="w-full" />
        </el-form-item>
        <el-form-item label="备注" prop="transport_type" class="col-span-full">
          <el-input v-model="form.basic.remark" placeholder="请输入" type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }" />
        </el-form-item>
      </div>
      <div class="v-title mb-3">采购单信息</div>
      <vxe-table :data="form.purchase">
        <vxe-column title="采购单号/异常单号" min-width="140">
          <template #default="{ row }">
            <el-link type="primary">{{ row.purchase_no }}</el-link>
          </template>
        </vxe-column>
        <vxe-column title="sku" min-width="120">
          <template #default="{ row }">
            <el-link type="primary">{{ row.sku }}</el-link>
          </template>
        </vxe-column>
        <vxe-column title="收货数量" field="deliver_num" min-width="80"></vxe-column>
        <vxe-column title="质检数量" field="qa_num" min-width="80"></vxe-column>
        <vxe-column title="良品数" field="quality_num" min-width="80"></vxe-column>
        <vxe-column title="不良品数" field="reject_num" min-width="80"></vxe-column>
        <vxe-column title="入库数" field="inbound_num" min-width="80"></vxe-column>
        <vxe-column title="已退货数量" field="returned_num" min-width="100"></vxe-column>
        <vxe-column title="可退数量" field="can_return_num" min-width="80"></vxe-column>
        <vxe-column title="申请退货数量" min-width="120">
          <template #default="{ row }">
            <el-input-number v-model="row.return_num" controls-position="right" :min="0" :step="1" :precision="0"
              class="w-full" />
          </template>
        </vxe-column>
        <vxe-column title="操作" width="80">
          <template #default="{ rowIndex }">
            <el-button link type="danger" @click="form.purchase.splice(rowIndex, 1)">删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
import Area from '@/components/Area'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
const isLook = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (rg_id, look) => {
  isLook.value = look
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {
    basic: {},
    purchase: []
  }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()

  const res = await api.detail({ rg_id })
  form.value = res.data
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  api.edit(form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>